package com.mr.cai.main.utils;

import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.OutputStreamWriter;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2015/5/19.
 */
public class StoreUtils {
    public static final String PATH = MrCaiConstant.ROOT_DIR+"storage/";
    public static final String DATAINFO = PATH + "relevantData";
    public static boolean isSdcardAvailable() {
        boolean exist = Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
        return exist;
    }
    public static File getSdcardRootDirectory() {
        if (isSdcardAvailable()) {
            return Environment.getExternalStorageDirectory();
        }
        return null;
    }
    protected static class WriteDataClass {
        private String data;
        private File file;

        public String getData () {
            return data;
        }

        public void setData (String data) {
            this.data = data;
        }

        public File getFile () {
            return file;
        }

        public void setFile (File file) {
            this.file = file;
        }
    }
    /**
     * 保存相关数据到文件
     *
     * @param data
     * @param file
     */
    private static void writeTxtData(final String data, final File file) {
        WriteDataClass writeDataClassBean = new WriteDataClass();
        writeDataClassBean.setData(data);
        writeDataClassBean.setFile(file);
        Observable.just(writeDataClassBean).subscribeOn(Schedulers.io())
                .map(new Func1<WriteDataClass, String>() {
                    @Override
                    public String call (WriteDataClass writeDataClass) {
                        OutputStreamWriter out = null;
                        try {
                            out = new OutputStreamWriter(new FileOutputStream(writeDataClass.getFile()),
                                    "UTF-8");
                            out.write(writeDataClass.getData());
                            out.flush();
                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {
                            try {
                                if (out != null) {
                                    out.close();
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        return null;
                    }
                }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<String>() {
            @Override
            public void call (String s) {

            }
        });
    }
/**
     * 保存相关数据到文件
     *
     * @param data
     * @param file
     */
    private static void writeRelevantData(Context context, final String data, final File file) {
        writeTxtData(data, file);
    }

    /**
     * 从文件中读取数据
     *
     * @param file
     * @return
     */
    private static String readRelevantData(File file) {
        ObjectInputStream in = null;
        try {
            in = new ObjectInputStream(new FileInputStream(file));
            return (String) in.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static File getRelevantFile(String filename) {
        if (!isSdcardAvailable()) {
            return null;
        }
        try {
            File dir = new File(Environment.getExternalStorageDirectory(), DATAINFO);
            if (!dir.isDirectory()) {
                dir.mkdirs();
            }
            File file = new File(dir, filename);
            if (!file.exists()) {
                file.createNewFile();
            }
            return file;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String saveRelevantData(Context context, String filename, String data) {
        File file = getRelevantFile(filename);

        if (file != null && !TextUtils.isEmpty(data)) {
            writeRelevantData(context, data, file);
            return file.getAbsolutePath();
        } else {
            return null;
        }
    }
    public static String saveTxtData(String filename, String data) {
        File file = getRelevantFile(filename);

        if (file != null && !TextUtils.isEmpty(data)) {
            writeTxtData(data, file);
            return file.getAbsolutePath();
        } else {
            return null;
        }
    }
    public static String getRelevantData(String filename) {
        File file = getRelevantFile(filename);

        if (file != null) {
            return readRelevantData(file);
        }
        return null;
    }
}
